<?php

namespace app\shop\controller\business;

use think\Controller;
use think\Request;

class Address extends Controller
{
    protected $AddressModel = null;
    // 构造函数
    public function __construct()
    {
        // 手动继承父级的构造函数
        parent::__construct();

        // 加载模型
        $this->AddressModel = model('business.Address');
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $busid = $this->request->param('busid', '', 'trim');

            if (empty($busid)) {
                $this->error('id为空');
            }

            $business = $this->AddressModel->with(['provinces', 'citys', 'districts'])->where(['busid' => $busid])->order('id desc')->select();

            if (!$business) {
                $this->error($this->AddressModel->getError());
            }

            $this->success("查询地址成功", null, $business);
        }
    }

    //收货地址添加
    public function add()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();

            $busid = $params['busid'] ?? 0;

            $business = model('business.Business')->find($busid);

            if (!$business) {
                $this->error('用户不存在');
            }

            $code = $params['code'] ?? '';

            $path = model('Region')->where(['code' => $code])->value('parentpath');

            if (!$path) {
                $this->error('所选地区不存在');
            }

            $pathArr = explode(',', $path);

            $params['province'] = $pathArr[0] ?? null;
            $params['city'] = $pathArr[1] ?? null;
            $params['district'] = $pathArr[2] ?? null;

            // 删除前端传过的code
            unset($params['code']);

            $this->AddressModel->startTrans();

            $status = $params['status'] ?? 0;

            if ($status === 1) {
                // 默认收货地址
                $updateStatus = $this->AddressModel->where(['busid' => $business['id']])->update(['status' => 0]);

                if ($updateStatus === false) {
                    $this->error('更新所有收货地址状态失败');
                }
            }

            $result = $this->AddressModel->validate('common/business/Address')->save($params);

            if ($result === false) {
                $this->AddressModel->rollback();
                $this->error($this->AddressModel->getError());
            } else {
                $this->AddressModel->commit();
                $this->success('添加收货地址成功');
            }
        }
    }

    //地址编辑
    public function edit()
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param();

            $busid = $params['busid'] ?? 0;

            $business = model('business.Business')->find($busid);

            if (!$business) {
                $this->error('用户不存在');
            }

            $addrid = $params['id'] ?? '';

            $address = $this->AddressModel->where(['busid' => $busid, 'id' => $addrid])->find();

            if (!$address) {
                $this->error('该收货地址不存在');
            }

            $code = $params['code'] ?? '';

            $path = model('Region')->where(['code' => $code])->value('parentpath');

            if (!$path) {
                $this->error('所选地区不存在');
            }

            $pathArr = explode(',', $path);

            $params['province'] = $pathArr[0] ?? null;
            $params['city'] = $pathArr[1] ?? null;
            $params['district'] = $pathArr[2] ?? null;

            // 删除前端传过的code
            unset($params['code']);

            $this->AddressModel->startTrans();

            $status = $params['status'] ?? 0;

            if ($status === 1) {
                // 默认收货地址
                $updateStatus = $this->AddressModel->where(['busid' => $business['id']])->update(['status' => 0]);

                if ($updateStatus === false) {
                    $this->error('更新所有收货地址状态失败');
                }
            }

            $result = $this->AddressModel->validate('common/business/Address')->isUpdate(true)->save($params);

            if ($result === false) {
                $this->AddressModel->rollback();
                $this->error($this->AddressModel->getError());
            } else {
                $this->AddressModel->commit();
                $this->success('更新收货地址成功');
            }
        }
    }

    //编辑页面列表
    public function info()
    {
        if ($this->request->isAjax()) {
            $busid = $this->request->param('busid', 0, 'trim');
            $addrid = $this->request->param('addrid', 0, 'trim');

            $business = model('business.Business')->find($busid);

            if (!$business) {
                $this->error('用户不存在');
            }

            $address = $this->AddressModel->where(['id' => $addrid, 'busid' => $busid])->find();

            if ($address) {
                $this->success('查询收货地址详情成功', null, $address);
            } else {
                $this->error('该收货地址不存在');
            }
        }
    }


    public function del()
    {
        if ($this->request->isPost()) {
            $busid = $this->request->param('busid', 0, 'trim');
            $addrid = $this->request->param('addrid', 0, 'trim');

            $business = model('business.Business')->find($busid);

            if (!$business) {
                $this->error('用户不存在');
            }

            $address = $this->AddressModel->where(['id' => $addrid, 'busid' => $busid])->find();

            if (!$address) {
                $this->error('该收货地址不存在');
            }

            $result = $address->delete();

            if ($result === false) {
                $this->error('删除收货地址失败');
            } else {
                $this->success('删除收货地址成功');
            }
        }
    }

    public function selected()
    {
        if ($this->request->isPost()) {
            $params = $this->request->param();

            $busid = $params['busid'] ?? 0;

            $business = model('business.Business')->find($busid);

            if (!$business) {
                $this->error('用户不存在');
            }

            $addrid = $params['id'] ?? '';

            $address = $this->AddressModel->where(['busid' => $busid, 'id' => $addrid])->find();

            if (!$address) {
                $this->error('该收货地址不存在');
            }

            $this->AddressModel->startTrans();

            $updateStatus = $this->AddressModel->where(['busid' => $busid])->update(['status' => 0]);

            if ($updateStatus === false) {
                $this->error('更新所有收货地址状态失败');
            }

            $data = [
                'id' => $addrid,
                'status' => 1
            ];

            $result = $this->AddressModel->isUpdate(true)->save($data);

            if ($result === false) {
                $this->AddressModel->rollback();
                $this->error('设置默认收货地址失败');
            } else {
                $this->AddressModel->commit();
                $this->success('设置默认收货地址成功');
            }
        }
    }
}
